package com.nuanshui.heatedloan.dao.userinfo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import com.nuanshui.heatedloan.userinfo.model.UserInfo;
import com.nuanshui.heatedloan.extension.hibernate.HibernateUtils;
import com.nuanshui.heatedloan.util.ReadSqlUtil;



@Component
public class UserInfoDaoImpl implements UserInfoDaoCustom{
	@PersistenceContext
	private EntityManager em;

	@Autowired
	private HibernateUtils hibernateUtils;
	
	@Autowired
	private JdbcTemplate  jdbcTemplate;

	@Override
	@SuppressWarnings("unchecked")
	public Map selectCount() {
		final String selectRegisterUserCountSql=ReadSqlUtil.getInstance().readSql("userInfo.xml","selectRegisterUserCount");
		final String selectBoundPhoneUserCountSql=ReadSqlUtil.getInstance().readSql("userInfo.xml","selectBoundPhoneUserCount");
		final String selectRecommendUserCountSql=ReadSqlUtil.getInstance().readSql("userInfo.xml","selectRecommendUserCount");
		Map<String,Long> map=new HashMap();
		Long selectRegisterUserCount=hibernateUtils.queryRowCountBySql(selectRegisterUserCountSql,null);
		Long selectBoundPhoneUserCount=hibernateUtils.queryRowCountBySql(selectBoundPhoneUserCountSql,null);
		Long selectRecommendUserCount=hibernateUtils.queryRowCountBySql(selectRecommendUserCountSql,null);
		map.put("selectRegisterUserCount", selectRegisterUserCount);
		map.put("selectBoundPhoneUserCount", selectBoundPhoneUserCount);
		map.put("selectRecommendUserCount", selectRecommendUserCount);
		return map;
	}

	@Override
	public Page<UserInfo> selectRegistedUsers(Pageable page) {
		final String selectRegistedUsersSql=ReadSqlUtil.getInstance().readSql("userInfo.xml","selectRegisterUser");		
		return hibernateUtils.findBySql(selectRegistedUsersSql,
				UserInfo.class, false,
				page);
	}

	@Override
	public Page<UserInfo> selectBoundPhoneUsers(Pageable page) {
		final String selectBoundPhoneUsersSql=ReadSqlUtil.getInstance().readSql("userInfo.xml","selectBoundPhoneUser");		
		return hibernateUtils.findBySql(selectBoundPhoneUsersSql,
				UserInfo.class, false,
				page);
	}

	@Override
	public Page<UserInfo> selectRecommendUsers(Pageable page) {
		final String selectRecommendUsersSql=ReadSqlUtil.getInstance().readSql("userInfo.xml","selectRecommendUser");		
		return hibernateUtils.findBySql(selectRecommendUsersSql,
				UserInfo.class, false,
				page);
	}
	
	@Override
	public void deleteUserById( Long id ){
		String sql= "delete  from cms_security_user where id =" +id;
		jdbcTemplate.execute(sql);
	}

	@Override
	public List<String> findPermissionsByRoleId(Long id) {
		String sql =" select  permission  from cms_security_role_permission where role_id = " +id;
		 List<String> permissions = jdbcTemplate.queryForList(sql, String.class);
		return permissions;
		
	}

	@Override
	public void deleteRolesById(Long userId) {
		String sql ="delete from cms_security_user_role where user_id ="+userId;
		jdbcTemplate.execute(sql);
		
	}

	@Override
	public void insertRoles(Long userId,Integer roleId) {
		String sql="insert into cms_security_user_role(priority,user_id,role_id) values(99,"+userId+","+roleId+")";
		jdbcTemplate.execute(sql);
		
	}

}
